import 'dart:math';

import 'package:flutter/material.dart';

class RoomList extends StatefulWidget {
  RoomList({super.key, required this.building, required this.point});
  //接收小区信息和楼栋信息
  String point;
  String building;
  @override
  State<RoomList> createState() => _RoomListState();
}

class _RoomListState extends State<RoomList> {
  //定义房间信息数据
  Map data = {
    "point": "", //小区信息
    "building": "", //楼栋信息
    'rooms': [], // 随机产生的房间号
  };

  @override
  void initState() {
    super.initState();
    getRoomRandom();
  }

//生成随机房间号数据
  getRoomRandom() {
    // 1.随机产生房间个数
    Random rand = Random();
    int size = rand.nextInt(6) + 3;
    //2.遍历房间的个数产生房间号
    List temp = [];
    for (int i = 0; i < size; i++) {
      // 2.1:随机产生楼层:1-20楼
      int floor = rand.nextInt(20) + 1;
      //2.2:随机产生房间号:1-4
      int room = rand.nextInt(4) + 1;
      //2.3拼接楼层和房间号:X0X
      String roomNum = '${floor}0$room';
      //2.4:去重相同的层房间号
      if (temp.contains(roomNum)) {
        continue;
      }
      //2.5:添加到列表中
      temp.add(roomNum);
      //2.6 数组排序 小到大
      // 排序：将房间号字符串转为整数进行比较:compareTo进行比较
      temp.sort((a, b) => int.parse(a).compareTo(int.parse(b)));
    }

    setState(() {
      data = {
        'point': widget.point,
        'building': widget.building,
        'rooms': temp,
      };
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('选择房间'),
        backgroundColor: Colors.transparent,
      ),
      body: Container(
        child: ListView.separated(
          itemCount: data['rooms'] == null
              ? 0
              : data['rooms'].length, // 确保 itemCount 不为 null
          separatorBuilder: (BuildContext context, int index) {
            return const Divider(
              height: 2,
              color: Colors.grey,
            );
          },
          itemBuilder: (BuildContext context, int index) {
            if (index >= (data['rooms'] ?? []).length) {
              // 检查 index 是否越界
              return Container(); // 如果越界，返回一个空容器
            }
            return GestureDetector(
              //跳转至添加房屋页
              onTap: () {
                Navigator.pushNamed(context, '/house_form', arguments: {
                  'point': data['point'],
                  'building': data['building'],
                  'room': data['rooms'][index]
                });
              },
              child: Container(
                padding: const EdgeInsets.all(10),
                height: 50,
                color: Colors.white,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                        '${data['point']}${data['building']}${data['rooms'][index]}'),
                    const Icon(
                      Icons.arrow_forward_ios,
                      size: 20,
                    )
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}
